package com.example.basic.bytes;

import com.sun.istack.internal.Nullable;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Test2 {

    public static void main(String[] args) throws Exception {
        File file = new File("./data.data");
        BufferedReader fin = new BufferedReader(new FileReader(file));
        String content = fin.readLine();
        //去头尾
        String substring = content.substring(2);
        content = substring.substring(0,substring.length()-2);

        byte point = (byte) (",".charAt(0) - 0);
        String pointstr = Integer.toHexString(point).toUpperCase();
        System.out.println("pointstr = " + pointstr);

        int indexOf = -1;
        for (int i = 0; i < 3; i++) {
            indexOf = content.indexOf(pointstr,indexOf + 1);
            System.out.println("indexOf = " + indexOf);
        }

        //分隔协议内容和文件内容
        String proContent = content.substring(0, indexOf + 2);
        System.out.println("proContent = " + proContent);
        byte[] proContentArray = new byte[proContent.length()/2];

        int index = 0;

        for (int i = 0; i < proContent.length();) {
            String substring1 = proContent.substring(i, i + 2);
            System.out.println("substring1 = " + substring1);
            int value = Integer.parseInt(substring1, 16);
            System.out.println("value = " + value);
            proContentArray[index] = (byte) value;
            i = i+2;
            index++;
        }

        System.out.println(Arrays.toString(proContentArray));
        String proText = new String(proContentArray, StandardCharsets.UTF_8);
        System.out.println("proText = " + proText);

        String fileContent = content.substring(indexOf + 2);
        System.out.println(fileContent);

        /**
         * 0X7D --> 0X7D 0X01
         * 0X5B --> 0X7D 0X02
         * 0X5D --> 0X7D 0X03
         * 0X2C --> 0X7D 0X04
         * 0X2A --> 0X7D 0X05
         */
        fileContent = fileContent.replaceAll("7D01","7D");
        fileContent = fileContent.replaceAll("7D02","5B");
        fileContent = fileContent.replaceAll("7D03","5D");
        fileContent = fileContent.replaceAll("7D04","2C");
        fileContent = fileContent.replaceAll("7D05","2A");


        byte[] fileContentArray = new byte[fileContent.length()/2];

        index = 0;

        System.out.println("转存图像-------------------");
        for (int i = 0; i < fileContent.length()-2;) {
            String substring1 = fileContent.substring(i, i + 2);
            System.out.println("substring1 = " + substring1);
            int value = Integer.parseInt(substring1, 16);
            System.out.println("value = " + value);
            fileContentArray[index] = (byte) value;
            System.out.println(fileContentArray[index]);
            i = i + 2;
            index++;
        }


        File dst = new File("./file1.jpg");
        if (!dst.exists()) {
            dst.createNewFile();
        }

        FileOutputStream fout = new FileOutputStream(dst);
//        for (byte b : fileContentArray) {
//            fout.write(transCode(new byte[]{b}));
//        }

        byte[] temp = new byte[2];

//        for (int i = 0; i < fileContentArray.length - 1; i++ ) {
//            temp[0] = fileContentArray[i];
//            temp[1] = fileContentArray[i+1];
//            fout.write(transCode(temp));
//        }

        fout.write(fileContentArray);

        fout.flush();
        fout.close();

    }

    @Nullable
    public static byte[] transCode(byte[] content) {
        if (content.length != 2) {
            throw new IllegalArgumentException("error");
        }

        byte a = content[0];
        byte b = content[1];

        //0X7D
        if (a == 125 && b == 1) {
            System.out.println("满足条件1------------");
            return new byte[]{125};
        }

        //0X5B
        if (a == 125 && b ==2) {
            System.out.println("满足条件2------------");
//            return new byte[]{125, 2};
            return new byte[]{91};
        }

        //0X5D
        if (a == 125 && b ==3) {
            System.out.println("满足条件3------------");
//            return new byte[]{125, 3};
            return new byte[]{93};
        }

        //0X2C
        if (a == 125 && b ==4) {
            System.out.println("满足条件4------------");
//            return new byte[]{125, 4};
            return new byte[]{44};
        }

        //0X2A
        if (a == 125 && b ==5) {
            System.out.println("满足条件5------------");
//            return new byte[]{125, 5};
            return new byte[]{42};
        }

        return content;
    }

}
